﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FileMonitor.Configuration.Tool.Controls.Prototype;

namespace FileMonitor.Configuration.Tool.Controls
{
    public partial class ProcedureProcessEditor :ProcessEditorBase
    {
        public ProcedureProcessEditor():base()
        {
            InitializeComponent();
            this.txt_ProcessName.Text = "Procedure_";
            this.txt_ProcessName.TextChanged += new EventHandler(txt_ProcessName_TextChanged);
            this.txt_TimeOut.TextChanged += new EventHandler(txt_TimeOut_TextChanged);
            this.txt_NextStatus.TextChanged += new EventHandler(txt_NextStatus_TextChanged);
            this.txt_BizProcedure.LostFocus += new EventHandler(txt_BizProcedure_LostFocus);
            this.txt_TargetProcedure.LostFocus += new EventHandler(txt_TargetProcedure_LostFocus);
        }

        void txt_BizProcedure_LostFocus(object sender, EventArgs e)
        {
            string dbName = this.box_SourceDatabaseName.Text;
            if (!Utility.FindProcedure(dbName, this.txt_BizProcedure.Text))
            {
                this.lblBizProcedureError.Visible = true;
                base.IsCanbeSaved = false;
            }
            else
            {
                this.lblBizProcedureError.Visible = false;
                base.IsCanbeSaved = true;
            }
        }

        void txt_TargetProcedure_LostFocus(object sender, EventArgs e)
        {
            string dbName = this.box_TargetDatabaseName.Text;
            if (!Utility.FindProcedure(dbName, this.txt_TargetProcedure.Text))
            {
                this.lblTargetProcedureError.Visible = true;
                base.IsCanbeSaved = false;
            }
            else
            {
                this.lblTargetProcedureError.Visible = false;
                base.IsCanbeSaved = true;
            }
        }

        void txt_NextStatus_TextChanged(object sender, EventArgs e)
        {
            string status = this.txt_NextStatus.Text;
            int i = 0;
            if (!Int32.TryParse(status, out i))
            {
                this.lblNextStatuError.Visible = true;
                base.IsCanbeSaved = false;
            }
            else
            {
                this.lblNextStatuError.Visible = false;
                base.IsCanbeSaved = true;
            }
        }

        void txt_TimeOut_TextChanged(object sender, EventArgs e)
        {
            string timeOut = this.txt_TimeOut.Text;
            int i=0;
            if (!Int32.TryParse(timeOut, out i))
            {
                this.lblTimeOutError.Visible = true;
                base.IsCanbeSaved = false;
            }
            else
            {
                this.lblTimeOutError.Visible = false;
                base.IsCanbeSaved = true;
            }
        }

        void txt_ProcessName_TextChanged(object sender, EventArgs e)
        {
            string text = this.txt_ProcessName.Text;
            if (!text.StartsWith("Procedure_"))
            {
                MessageBox.Show("Process Name have to start with 'Procedure_'");
                base.IsCanbeSaved = false;
            }
            else
            {
                base.IsCanbeSaved = true;
            }
        }

        public void BindTypeList(List<string> list)
        {
            this.box_TypeName.DataSource = list;
        }

        public void BindDatabaseList(List<string> list)
        {
            this.box_TargetDatabaseName.DataSource = list.ToArray();
            this.box_SourceDatabaseName.DataSource = list;
        }
    }
}
